package cn.itcast.service.impl;

import cn.itcast.dao.PromotionRepository;
import cn.itcast.domain.delivery.Promotion;
import cn.itcast.domain.page.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class PromotionServiceImpl implements cn.itcast.service.PromotionService {
    @Autowired
    private PromotionRepository promotionRepository;
    @Override
    @Transactional
    public void save(Promotion promotion) {
        promotionRepository.save(promotion);
    }

    @Override
    public Page<Promotion> findPageData(Pageable pageable) {
        return promotionRepository.findAll(pageable);
    }

    @Override
    public PageBean<Promotion> findPageData(Integer page, Integer rows) {
        Pageable pageable=new PageRequest(page-1,rows);
        Page<Promotion> pageData = promotionRepository.findAll(pageable);
        PageBean<Promotion>pageBean=new PageBean<>();
        pageBean.setTotalCount(pageData.getTotalElements());
        pageBean.setPageData(pageData.getContent());
        return pageBean;
    }

    @Override
    public Promotion getDetails(Integer id) {
        return promotionRepository.findOne(id);
    }

    @Override
    @Transactional
    public void updataStatus(Date now) {
        promotionRepository.updataStatus(now);
    }
}
